package com.example.novelweb.config;


import com.example.novelweb.constant.UserConstant;
import com.example.novelweb.interceptor.LoginInterceptor;
import com.example.novelweb.interceptor.RefreshTokenInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.annotation.Resource;
import java.nio.file.Path;
import java.nio.file.Paths;

@Configuration
public class MvcConfig implements WebMvcConfigurer {
  @Resource
  private StringRedisTemplate stringRedisTemplate;

  @Override
  public void addInterceptors(InterceptorRegistry registry) {
    // order 0
    registry.addInterceptor(new RefreshTokenInterceptor(stringRedisTemplate))
        .addPathPatterns("/**");

//    // order 1
//    // Pattern match by @LoginCheck
//    registry.addInterceptor(new LoginInterceptor()).addPathPatterns("/**");

  }

  private final Path imgPath = Paths.get(UserConstant.IMG_RES_PATH).toAbsolutePath().normalize();
  @Override
  public void addResourceHandlers(ResourceHandlerRegistry registry) {
    registry.addResourceHandler("/images/**")
        .addResourceLocations("file:"+ imgPath + "/");
  }

}
